package com.ruoyi.warning.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.warning.domain.entity.Device;
import com.ruoyi.warning.domain.entity.Links;
import com.ruoyi.warning.domain.query.LinkQuery;
import com.ruoyi.warning.domain.vo.LinkVO;
import com.ruoyi.warning.mapper.DevicesMapper;
import com.ruoyi.warning.mapper.LinksMapper;
import com.ruoyi.warning.service.IDevicesService;
import com.ruoyi.warning.service.ILinksService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chenhc
 * @since 2025-04-07
 */
@Service
public class LinksServiceImpl extends ServiceImpl<LinksMapper, Links> implements ILinksService {

    @Resource
    private LinksMapper linksMapper;
    @Resource
    private IDevicesService deviceService;
    @Resource
    private DevicesMapper devicesMapper;
    @Override
    public Object getNetworkTopology() {
        // 找links
        List<LinkVO> links = linksMapper.getLinkVOList(null);
        List<LinkVO> linksM = linksMapper.getLinkVOManualList(null);
        List<LinkVO> linksC = linksMapper.getLinkVOManualCloudList(null);
        links.addAll(linksM);
        links.addAll(linksC);
        List<Device> devices = devicesMapper.list();
        Map<String, Object> result = new HashMap<>();
        result.put("links", links);
        result.put("devices", devices);
        return result;
    }

    @Override
    public Object getNetworkTopologyById(String id) {
        List<LinkVO> links = linksMapper.getLinkVOList(id);
        List<LinkVO> linksM = linksMapper.getLinkVOManualList(id);
        links.addAll(linksM);
        Map<String, Object> result = new HashMap<>();
//        if (links.isEmpty()) {
//            LambdaQueryWrapper<Device> wrapper = new QueryWrapper<Device>().lambda()
//                    .eq(Device::getDeviceId, id);
//            List<Device> devices = deviceService.list(wrapper);
//            result.put("devices", devices);
//            return result;
//        }
        LambdaQueryWrapper<Device> wrapper = new QueryWrapper<Device>().lambda()
                .in(!links.isEmpty(),Device::getDeviceId, links.stream().map(LinkVO::getLocalDeviceId).toList())
                .or()
                .in(!links.isEmpty(),Device::getDeviceId, links.stream().map(LinkVO::getRemoteDeviceId).toList())
                .or()
                .eq(Device::getDeviceId, id);
        List<Device> devices = deviceService.list(wrapper);
        result.put("links", links);
        result.put("devices", devices);
        return result;
    }

    @Override
    public Object getCriticalLink(LinkQuery query) {
        Page<Links> page = PageHelper.startPage(query.getPageNum(),query.getPageSize());
        List<LinkVO> links = linksMapper.getCriticalLink(query);
        TableDataInfo tableDataInfo = new TableDataInfo(links, page.getTotal());
        return tableDataInfo;
    }

    @Override
    public void addOrUpdateCriticalLink(Links links) {
        //检查是否存在
        List<LinkVO> list = linksMapper.getLinkList(links);
        //如果没有就新增
        if(list.isEmpty()){
            linksMapper.addLink(links);
        }else{
            Boolean flag = false;
            //如果有就更新，优先更新自带表
            for(Links link:list){
                if("0".equals(link.getManual())){
                    link.setSysname(links.getSysname());
                    link.setIsCritical(1);
                    this.updateById(link);
                    flag = true;
                    break;
                }else {
                    links.setId(link.getId());
                }
            }
            if(!flag){
                linksMapper.updateLink(links);
            }
        }
    }
}
